<template>
    <!--                                    自定义标题-->
    <div class="me-allct-body" v-title :data-title="categoryTagTitle" >
        <el-container class="me-allct-container">
            <el-main>
                <el-tabs v-model="activeName">

                    <el-tab-pane label="文章分类" name="category">
                        <ul class="me-allct-items">
                            <li v-for="category in categorys" @click="view(category.id)" :key="category.id" class="me-allct-item">
                                <div class="me-allct-content">
                                    <a class="me-allct-info">
                                        <!--                             有标签展示，没标签默认-->
                                        <img class="me-allct-img" :src="category.avatar?category.avatar:defaultAvatar"/>
                                        <h4 class="me-allct-name">{{category.categoryName}}</h4>
                                        <p class="me-allct-description">{{category.description}}</p>
                                    </a>

                                    <div class="me-allct-meta">
                                        <span>{{category.articles}} 文章</span>
                                    </div>
                                </div>
                            </li>
                        </ul>
                    </el-tab-pane>

                    <el-tab-pane label="标签" name="tag">
                        <ul class="me-allct-items">
                            <li v-for="t in tags" @click="view(t.id)" :key="t.id" class="me-allct-item">
                                <div class="me-allct-content">
                                    <a class="me-allct-info">
                                        <img class="me-allct-img" :src="t.avatar?t.avatar:defaultAvatar"/>
                                        <h4 class="me-allct-name">{{t.tagName}}</h4>
                                    </a>

                                    <div class="me-allct-meta">
                                        <span>{{t.articles}}  文章</span>
                                    </div>
                                </div>
                            </li>
                        </ul>
                    </el-tab-pane>
                </el-tabs>
            </el-main>
        </el-container>
    </div>
</template>

<script>
    import defaultAvatar from '@/assets/img/logo.png'
    import {listTagsDetail} from "@/api/tag"
    import {listCategorysDetail} from "@/api/category"

    export default {
        name: "BlogAllCategoryTag",
        data(){
            return {
                //种类
                categorys: [],
                //标签
                tags: [],
                defaultAvatar : defaultAvatar,
                //选中的标题
                activeNameTitle: 'category'
            }
        },
        computed:{
            //关联标题和选中的标题
            activeName: {
                get(){
                    return this.$route.params.type;
                },
                set(newValue){
                    console.log( this.$router)
                    this.$router.push({path: `/${newValue}/all`})
                    this.activeNameTitle = newValue
                }
            },
            categoryTagTitle() {
                if(this.activeNameTitle == 'category'){
                    return "文章分类-码神之路"
                }else{
                    return "标签-码神之路"
                }
            }
        },
        created(){
            this.listCategorys();
            this.listTags();
        },
        methods:{
            //获取全部的分类列表
            listCategorys(){
                //发起http请求 请求后端的数据
                listCategorysDetail().then((res)=>{
                    ////res.data = Result (success,msg,data)
                    if(res.data.success){
                        this.categorys = res.data.data;
                    }else{
                        this.$message.error(res.data.msg);
                    }
                }).catch((err)=>{
                    this.$message.error("系统错误");
                }).finally(()=>{
                })
            },
            //获取全部的标签列表
            listTags(){
                //发起http请求 请求后端的数据
                listTagsDetail().then((res)=>{
                    ////res.data = Result (success,msg,data)
                    if(res.data.success){
                        this.tags = res.data.data;
                    }else{
                        this.$message.error(res.data.msg);
                    }
                }).catch((err)=>{
                    this.$message.error("系统错误");
                }).finally(()=>{
                })
            },
            //详情
            view(id) {
                this.$router.push({path: `/${this.activeName}/${id}`})
            },
        }
    }
</script>

<style>
    .me-allct-body {
        margin: 60px auto 140px;
    }

    .me-allct-container {
        width: 1000px;
    }

    .me-allct-items {
        padding-top: 2rem;
    }

    .me-allct-item {
        width: 25%;
        display: inline-block;
        margin-bottom: 2.4rem;
        padding: 0 .7rem;
        box-sizing: border-box;
    }

    .me-allct-content {
        display: inline-block;
        width: 100%;
        background-color: #fff;
        border: 1px solid #f1f1f1;
        transition: border-color .3s;
        text-align: center;
        padding: 1.5rem 0;
    }

    .me-allct-info {
        cursor: pointer;
    }

    .me-allct-img {
        margin: -40px 0 10px;
        width: 60px;
        height: 60px;
        vertical-align: middle;

    }

    .me-allct-name {
        font-size: 21px;
        font-weight: 150;
        overflow: hidden;
        text-overflow: ellipsis;
        white-space: nowrap;
        margin-top: 4px;
    }

    .me-allct-description {
        min-height: 50px;
        font-size: 13px;
        line-height: 25px;
    }

    .me-allct-meta {
        font-size: 12px;
        color: #969696;
    }
</style>
